package com.example.langchain4j_spring.config;

import com.example.langchain4j_spring.service.ToolsService;
import dev.langchain4j.community.model.dashscope.QwenEmbeddingModel;
import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.memory.chat.ChatMemoryProvider;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.chat.StreamingChatLanguageModel;
import dev.langchain4j.rag.content.retriever.ContentRetriever;
import dev.langchain4j.rag.content.retriever.EmbeddingStoreContentRetriever;
import dev.langchain4j.service.*;
import dev.langchain4j.store.embedding.EmbeddingStore;
import dev.langchain4j.store.embedding.inmemory.InMemoryEmbeddingStore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author: zjg
 * @Date: 2025/5/13 10:31
 * @Description:
 **/


@Configuration
public class AiConfig {

    public  interface Assistant {
        String chat(String question);

        TokenStream stream(String question);
    }

    @Bean
    public Assistant assistant(ChatLanguageModel chatLanguageModel,
                               StreamingChatLanguageModel streamingChatLanguageModel) {

        ChatMemory chatMemory = MessageWindowChatMemory.withMaxMessages(10);


        // 为Assistant构建动态代理对象 chat --> 对话内容存储ChatMemory stream --> 取出memory中的聊天记录 --> 放入到当前对话中
        Assistant assistant = AiServices.builder(Assistant.class).chatLanguageModel(chatLanguageModel)
                .streamingChatLanguageModel(streamingChatLanguageModel)
                .chatMemory(chatMemory)
                .build();

        return assistant;
    }


    public  interface AssistantUnique {
        String chat(@MemoryId int memoryId,@UserMessage String question);

        TokenStream stream(@MemoryId int memoryId,@UserMessage String question);

        @SystemMessage("""
                您是“智彩的客户聊天支持代理。请以友好、乐于助人且愉快的方式来回复。
                   您正在通过在线聊天系统与客户互动，要多说中奖发大财的话语。
                   您可以在线提供生成双色球彩票的号码 红色球1-33 篮球为1-16  红色球6位数  蓝色球1位数。
                   请讲中文。
                   今天的日期是{{current_date}}
                """)
        TokenStream stream(@MemoryId int memoryId,@UserMessage String question,@V("current_date")String currentDate);
    }

    public  interface AssistantUniqueStore {
        String chat(@MemoryId int memoryId,@UserMessage String question);

        TokenStream stream(@MemoryId int memoryId,@UserMessage String question);
    }

    @Bean
    public AssistantUnique assistantUnique(ChatLanguageModel chatLanguageModel,
                                           StreamingChatLanguageModel streamingChatLanguageModel,
                                           ToolsService toolsService) {



        // 为Assistant构建动态代理对象 chat --> 对话内容存储ChatMemory stream --> 取出memory中的聊天记录 --> 放入到当前对话中
        AssistantUnique assistantUnique = AiServices.builder(AssistantUnique.class).chatLanguageModel(chatLanguageModel)
                .streamingChatLanguageModel(streamingChatLanguageModel)
                .chatMemoryProvider(memoryId -> MessageWindowChatMemory.builder().maxMessages(10).id(memoryId).build())
                .tools(toolsService)
                .build();

        return assistantUnique;
    }


    @Bean
    public EmbeddingStore embeddingStore() {
        return new InMemoryEmbeddingStore();
    }



    // 对话记录持久化
    @Bean
    public AssistantUniqueStore assistantUniqueStore(ChatLanguageModel chatLanguageModel,
                                                     StreamingChatLanguageModel streamingChatLanguageModel,
                                                     EmbeddingStore embeddingStore,
                                                     QwenEmbeddingModel embeddingModel) {


        // 可以构建数据库或redis 将数据绑定到ChatMemory中
        PersistentChatMemoryStore store = new PersistentChatMemoryStore();
        ChatMemoryProvider chatMemoryProvider= memoryId -> MessageWindowChatMemory.builder()
                .maxMessages(10)
                .id(memoryId)
                .chatMemoryStore(store)
                .build();

        ContentRetriever retriever = EmbeddingStoreContentRetriever.builder()
                .embeddingStore(embeddingStore)
                .embeddingModel(embeddingModel)
                .maxResults(1)
                .minScore(0.6)
                .build();
        // 为Assistant构建动态代理对象 chat --> 对话内容存储ChatMemory stream --> 取出memory中的聊天记录 --> 放入到当前对话中
        AssistantUniqueStore assistantUnique = AiServices.builder(AssistantUniqueStore.class).
                chatLanguageModel(chatLanguageModel)
                .contentRetriever(retriever)
                .streamingChatLanguageModel(streamingChatLanguageModel)
                .chatMemoryProvider(chatMemoryProvider)
                .build();

        return assistantUnique;
    }

    // RAG 向量化增强检索

}
